from django.http import HttpResponse
import os

def hello(request):
    return HttpResponse("Hello world ! ")

from django.shortcuts import render

def homepage(request):
    views_list = ["<a href='https://gitee.com/luyi07/comphys'>计算物理Gitee仓库</a>",\
            "<a href='https://gitee.com/luyi07/mathphys'>数学物理方法Gitee仓库</a>",\
            "<a href='http://127.0.0.1:8000/comphys06方程求根'>ComPhys06方程求根</a>"]
    return render(request, "homepage.html", {"views_list": views_list})

def pdf_view(request, filename):
    with open(filename, 'r') as pdf:
        response = HttpResponse(pdf.read(), mimetype = 'application/pdf')
        response['Content-Disposition'] = 'inline;filename=some_file.pdf'
        return response
    pdf.closed

def comphys06(request):
    context={}
    return render(request, "ComPhys06方程求根.html", context)

def jianguo(request):
    dir = "statics/works/"
    authors = os.listdir(dir); print("jianguo authors = ", authors)
    views_list = []
    for author in authors:
        if author == '熊猫酒仙':
            views_list += [ "<a href='pandawarrior/'>" + author + "</a>"]
        else:
            views_list += [ "<a href='" + author + "/'>" + author + "</a>"]
    return render(request, "jianguo.html", {"views_list": views_list})

def collection(request, author):
    print("author = ", author)
    dir = "statics/works/" + author + "/"
    filenames = os.listdir(dir)
    print("filenames = ", filenames)
    content = []
    for filename in filenames:
        content += [ line for line in open(dir+filename, 'r', encoding='UTF-8')]
        print("open = ", open(dir+filename, 'r', encoding='UTF-8') )
        content += [" "," "]
    print("content = ", content)
    context = {
        'author': author,
        'content': content,
    }
    return render(request, 'collection.html', context)

def binbin(request):
    context = {}
    context['hello'] = '张老师，你好吗？你晕车吗？'
    return render(request, 'runoob.html', context)
